
' Files
Include "TCursor.bmx"

' TWidgetSkin
Type TWidgetSkin
	Field dir:String
	Field name:String
	
	Field c:TImage								' Center (fill)
	Field n:TImage, s:TImage, w:TImage, e:TImage		' North, South, West, East
	Field nw:TImage, ne:TImage					' North
	Field sw:TImage, se:TImage					' South
	
	' Buttons
	Field btnClose:TImage
	
	Field textOffsetX:Int
	Field textOffsetY:Int
	
	' Init
	Method Init(nName:String, nDir:String)
		Self.name = nName
		Self.dir = nDir
		
		TGUI.logger.Write("Loading widget skin '" + Self.name + "' from '" + Self.dir + "'")
		
		Local prefix:String = nDir + nName + "/" + nName + "-"
		Local postfix:String = "." + TSkin.extension
		
		' TODO: Remove hardcoded stuff
		SetMaskColor(255, 0, 255)
		
		Self.c = Log_LoadImage(TGUI.logger, prefix + "C" + postfix)
		Self.n = Log_LoadImage(TGUI.logger, prefix + "N" + postfix)
		Self.s = Log_LoadImage(TGUI.logger, prefix + "S" + postfix)
		Self.w = Log_LoadImage(TGUI.logger, prefix + "W" + postfix)
		Self.e = Log_LoadImage(TGUI.logger, prefix + "E" + postfix)
		Self.nw = Log_LoadImage(TGUI.logger, prefix + "NW" + postfix)
		Self.ne = Log_LoadImage(TGUI.logger, prefix + "NE" + postfix)
		Self.sw = Log_LoadImage(TGUI.logger, prefix + "SW" + postfix)
		Self.se = Log_LoadImage(TGUI.logger, prefix + "SE" + postfix)
		
		Self.btnClose = Log_LoadImage(TGUI.logger, prefix + "Button-Close" + postfix)
		
		If Self.ne
			SetImageHandle Self.ne, Self.ne.width, 0
		EndIf
		If Self.sw
			SetImageHandle Self.sw, 0, Self.sw.height
		EndIf
		If Self.se
			SetImageHandle Self.se, Self.se.width, Self.se.height
		EndIf
		
		' Load INI
		Local ini:TINI = TINI.Create(nDir + nName + "/" + nName + ".ini")
		If ini.Load()
			Self.textOffsetX = Int(ini.Get("Widget", "TextOffsetX"))
			Self.textOffsetY = Int(ini.Get("Widget", "TextOffsetY"))
		EndIf
	End Method
	
	' ToString
	Method ToString:String()
		Return Self.name
	End Method
	
	' Create
	Function Create:TWidgetSkin(nName:String, nDir:String)
		Local widgetSkin:TWidgetSkin = New TWidgetSkin
		widgetSkin.Init(nName, nDir)
		Return widgetSkin
	End Function
End Type

' TSkin
Type TSkin
	Global dir:String
	Global extension:String
	
	' Skin name
	Field name:String
	Field localDir:String
	
	' Cursors
	Field cursors:TMap
	
	' Widgets
	Field window:TWidgetSkin
	Field checkBox:TWidgetSkin
	
	' Init
	Method Init(nName:String)
		TGUI.logger.Write("Initializing skin '" + nName + "'")
		
		Self.cursors = CreateMap()
		Self.name = nName
		
		Self.localDir = TSkin.dir + Self.name + "/"
		Self.window = TWidgetSkin.Create("Window", Self.localDir)
		Self.checkBox = TWidgetSkin.Create("CheckBox", Self.localDir)
		
		Self.LoadCursors()
	End Method
	
	' LoadCursors
	Method LoadCursors()
		TGUI.logger.Write("Loading cursors")
		
		Local files:String[]
		Local file:String
		
		files = LoadDir(Self.localDir + TCursor.dir)
		For file = EachIn files
			If ExtractExt(file).ToLower() = TSkin.extension
				' Load cursor
				Self.AddCursor(StripExt(file))
			EndIf
		Next
	End Method
	
	' AddCursor
	Method AddCursor(nName:String)
		Self.cursors.Insert(nName, TCursor.Create(nName, Log_LoadImage(TGUI.logger, Self.localDir + TCursor.dir + nName + "." + TSkin.extension)))
	End Method
	
	' GetCursor
	Method GetCursor:TCursor(name:String)
		Return TCursor(cursors.ValueForKey(name))
	End Method
	
	' ToString
	Method ToString:String()
		Return Self.name
	End Method
	
	' InitClass
	Function InitClass(skinsDir:String, skinsExt:String = "png")
		TSkin.dir = skinsDir
		TSkin.extension = skinsExt
		
		TCursor.InitClass("Cursors/")
	End Function
	
	' Create
	Function Create:TSkin(nName:String)
		Local skin:TSkin = New TSkin
		skin.Init(nName)
		Return skin
	End Function
End Type
